﻿using System;

namespace LinqToOwin
{
  public static class Html
  {
    public static string Encode(string text)
    {
      return text.Replace(">", "&gt;").Replace("<", "&lt;").Replace("\"", "&quot;");
    }

    public static string Begin(string head = null, string body = null)
    {
      return "<!DOCTYPE html><html><head>" + head + "</head><body>" + body;
    }

    public static string End(string body = null)
    {
      return body + "</body></html>";
    }

    public static string Form(string method = "post", string action = null, string body = null)
    {
      return action == null
           ? @"<form method=""" + method + @""">" + body + "</form>"
           : @"<form method=""" + method + @""" action=""" + action + @""">" + body + "</form>";
    }

    public static string TextField(string name = null, string label = null, string value = null)
    {
      return InputField("text", name, label, value);
    }

    public static string PasswordField(string name = null, string label = null, string value = null)
    {
      return InputField("password", name, label, value);
    }

    public static string SubmitField(string name = null, string value = "Submit")
    {
      return InputField("submit", name, value: value);
    }

    public static string InputField(string type = "text", string name = null, string label = null, string value = null)
    {
      return name == null
           ? label == null
             ? @"<input type=""" + type + @""" value=""" + value + @""" />"
             : "<span>" + label + @"</span> <input type=""" + type + @""" value=""" + value + @""" />"
           : label == null
             ? @"<input type=""" + type + @""" id=""" + name + @""" name=""" + name + @""" value=""" + value + @""" />"
             : @"<label for=""" + name + @""">" + label + @"</label> <input type=""" + type + @""" id=""" + name + @""" name=""" + name + @""" value=""" + value + @""" />";
    }

    public static string Link(string href, string text, string target = null)
    {
      return @"<a href=""" + href + @""" target=""" + target + @""" >" + text + "</a>";
    }

    public static string ClientButtonField(string text, string onClick, string @class = null)
    {
      return @"<button class=""" + @class + @""" type=""button"" onclick=""" + onClick + @""">" + text + "</button>";
    }

    public static string Image(string source)
    {
      return @"<img src=""" + source + @"""/>";
    }

    public static string Image(byte[] source)
    {
      return @"<img src=""data:image;base64," + Convert.ToBase64String(source, Base64FormattingOptions.None) + @"""/>";
    }
  }
}